<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/4
 * Time: 17:24
 */

namespace app\jiaxiaotong\controller;


use cmf\controller\HomeBaseController;
use cmf\controller\UserBaseController;
use think\Db;
use think\Request;

class CommonController extends UserBaseController
{

    public  function __construct(Request $request)
    {
        parent::__construct($request);
    }

    /**
     * 获取当前用户孩子 --当前学生所有的id
     * @return int
     */
    public static  function cmf_get_current_student_id()
    {
       $p_id=cmf_get_current_user_id();
        if(empty($p_id))
        {
            return 0;
        }else
        {
            $students=Db::name('student')->where(['delete_time'=>0,'u_id'=>$p_id])->order('id asc')->select();
            if(count($students->toArray())==0)
            {
                return redirect('jiaxiaotong/nochild');
            }
            session('students',$students);
        }
        return $students;
    }
    /*
     * 1.如果父母没有选择孩子，默认是第一个孩子
     * 2.父母没有添加孩子，点击提示去添加孩子或者显示没有可以查看的孩子
     */
    public  static  function first_student()
    {
        $p_id=cmf_get_current_user_id();
        $students=Db::name('student')->where(['delete_time'=>0,'u_id'=>$p_id])->select();
        //没有孩子
        if(count($students->toArray())==0)
        {
            return redirect('jiaxiaotong/nochild');
        }else{
            $student_id=Db::name('student')->where(['delete_time'=>0,'u_id'=>$p_id])->order('id')->value('id');
            return $student_id;
        }

    }
    /*
     * 1.如果父母没有选择孩子，默认是第一个孩子
     * 2.父母没有添加孩子，点击提示去添加孩子或者显示没有可以查看的孩子
     */
    public function student()
    {
        $p_id=cmf_get_current_user_id();
        $students=Db::name('student')->where(['delete_time'=>0,'u_id'=>$p_id])->order('id asc')->select();
        if(count($students->toArray())==0)
        {
            return $this->redirect('order/order/add_student');
        }
        $this->assign('students',$students);
        if(!empty($_SESSION['student_id']))
        {
            return $_SESSION['student_id'];
        }else{
            $_SESSION['student_id']=Db::name('student')->where(['delete_time'=>0,'u_id'=>$p_id])->order('id')->value('id');
        }
        $this->assign('student_id', $_SESSION['student_id']);
        return  $_SESSION['student_id'];


    }
    /*
  * ajax_student
  */
    public  function ajax_student()
    {
        $student_id=$this->request->param('student_id',0);

        $p_id=cmf_get_current_user_id();
        $students=Db::name('student')->where(['delete_time'=>0,'u_id'=>$p_id])->order('id asc')->select();



        if(count($students->toArray())==0)
        {
            return redirect('jiaxiaotong/nochild');
        }
        $this->assign('students',$students);

        $_SESSION['student_id']=null;

        if(!empty($_SESSION['student_id']) && !empty($student_id))
        {
            $_SESSION['student_id']=$student_id;
        }elseif(empty($_SESSION['student_id']) && !empty($student_id)) {
            $_SESSION['student_id']=$student_id;
        }else{
                $_SESSION['student_id']=Db::name('student')->where(['delete_time'=>0,'u_id'=>$p_id])->order('id')->value('id');
       }
        $this->assign('student_id', $_SESSION['student_id']);
        return  $_SESSION['student_id'];


    }


}